﻿Imports System.Xml

Public Class frmInterest
    Public _dir As String
    Public statusnew As Boolean   ' false: Open; True: New
    Public path As String  ' path file xml
    Dim sdataset As DataSet
    Dim dTable As DataTable
    
    Private Sub btSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSave.Click
        Dim ext As String = ""
        sdataset = New DataSet
        sdataset.Tables.Add(dTable)

        If (statusnew = False) Then 'Open
            sdataset.WriteXml(path)
        Else 'New
            _dir = getUserAppFolder("ThungDoNghe", "QuanTam", "")
            Dim SaveFileDialog As New SaveFileDialog
            SaveFileDialog.InitialDirectory = _dir
            SaveFileDialog.Filter = "Text Files (*.qt)|*.qt|All Files (*.*)|*.*"
            If (SaveFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
                sdataset.WriteXml(SaveFileDialog.FileName)
            End If
        End If
    End Sub

    Private Sub frmInterest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'sdataset = New DataSet
        dTable = New DataTable
        dTable.Columns.Add("Title", GetType(String))
        dTable.Columns.Add("Problem", GetType(String))
        dTable.Columns.Add("Origin", GetType(String))
        dTable.Columns.Add("Datetime", GetType(DateTime))
        dTable.Columns.Add("Situation", GetType(String))
        If (statusnew = True) Then

        Else
            Dim xmlDatadoc As New XmlDataDocument
            xmlDatadoc.DataSet.ReadXml(path)
            For Each table As DataTable In xmlDatadoc.DataSet.Tables
                For Each row1 As DataRow In table.Rows
                    dTable.Rows.Add(row1(0), row1(1), row1(2), row1(3), row1(4))
                Next
            Next
            dgvResult.DataSource = dTable
        End If




        'sdataset.Tables.Add(dTable)
    End Sub

    Private Sub btAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAdd.Click
        dTable.Rows.Add(Title.Text, Problem.Text, Origin.Text, DateTime.Value, Situation.Text)
        dgvResult.DataSource = dTable
    End Sub

    Private Sub DateTime_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTime.ValueChanged

    End Sub

    Private Sub dgvResult_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvResult.SelectionChanged
        Try
            Title.Text = dgvResult(dgvResult.Columns("Title").Index, dgvResult.CurrentRow.Index).Value
            Problem.Text = dgvResult(dgvResult.Columns("Problem").Index, dgvResult.CurrentRow.Index).Value
            Origin.Text = dgvResult(dgvResult.Columns("Origin").Index, dgvResult.CurrentRow.Index).Value
            DateTime.Value = dgvResult(dgvResult.Columns("Datetime").Index, dgvResult.CurrentRow.Index).Value
            Situation.Text = dgvResult(dgvResult.Columns("Situation").Index, dgvResult.CurrentRow.Index).Value
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDelete.Click
        'dgvResult.Rows.RemoveAt(dgvResult.CurrentRow.Index)
        If MessageBox.Show("Are you sure you want to delete " & Title.Text & "?", "Delete row", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
            dTable.Rows.RemoveAt(dgvResult.CurrentRow.Index)
            dgvResult.DataSource = dTable
        End If
    End Sub

    Private Sub btUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btUpdate.Click
        dgvResult(dgvResult.Columns("Title").Index, dgvResult.CurrentRow.Index).Value = Title.Text
        dgvResult(dgvResult.Columns("Problem").Index, dgvResult.CurrentRow.Index).Value = Problem.Text
        dgvResult(dgvResult.Columns("Origin").Index, dgvResult.CurrentRow.Index).Value = Origin.Text
        dgvResult(dgvResult.Columns("Datetime").Index, dgvResult.CurrentRow.Index).Value = DateTime.Value
        dgvResult(dgvResult.Columns("Situation").Index, dgvResult.CurrentRow.Index).Value = Situation.Text

    End Sub
End Class